C++中 exception 的使用

您所在的位置:网站首页 exception ex C++中 exception 的使用

C++中 exception 的使用

2024-07-11 09:14| 来源: 网络整理| 查看: 265

std:: exception ,定义于头文件 ,它提供一致的接口,以通过 throw 表达式处理错误。

标准库所生成的所有异常继承自 std::exception。

其继承结构如下:

logic_error invalid_argumentdomain_errorlength_errorout_of_rangefuture_error(C++11)bad_optional_access(C++17) runtime_error range_erroroverflow_errorunderflow_errorregex_error(C++11)nonexistent_local_time(C++20)ambiguous_local_time(C++20)tx_exception(TM TS)system_error(C++11) ios_base::failure(C++11)filesystem::filesystem_error(C++17) bad_typeidbad_cast bad_any_cast(C++17) bad_weak_ptr(C++11)bad_function_call(C++11)bad_alloc bad_array_new_length(C++11) bad_exceptionios_base::failure(C++11 前)bad_variant_access(C++17) 成员函数

(构造函数)

构造异常对象 (公开成员函数)

(析构函数)

[虚]

析构该异常对象 (虚公开成员函数)

operator=

复制异常对象 (公开成员函数)

what

[虚]

返回解释性字符串 (虚公开成员函数)

 

示例:标准库exception和自定义exception的使用。 #include #include using namespace std; const string egg="I not like this number,so I decided to refuse it."; class non_44_error: public logic_error{ public: explicit non_44_error(const string &s=egg):logic_error(s){} }; int main(){ int input; while(1){ try{ cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3